/*
 * @lc app=leetcode.cn id=1779 lang=javascript
 *
 * [1779] 找到最近的有相同 X 或 Y 坐标的点
 */

// @lc code=start
/**
 * @param {number} x
 * @param {number} y
 * @param {number[][]} points
 * @return {number}
 */
var nearestValidPoint = function (x, y, points) {
  let min = Number.MAX_SAFE_INTEGER, idx = -1;
  points.forEach(([a, b], index) => {
    if (a === x || b === y) {
      const dis = Math.abs(a - x) + Math.abs(b - y);
      if (dis < min) {
        min = dis;
        idx = index;
      }
    }
  });
  return idx;
};
// @lc code=end

